package scala.demo

/**
 * 数组实现栈
 *  栈： 从头入栈  从头删除
 * @param n
 */
class ArrayStackT(n: Int) {


  var n1 = n
  var item = new Array[String](n1)
  var count: Int = 0

  /**
   * 入栈
   * @param i
   * @return Boolean
   */
  def push(i: String): Boolean = {
    if (count == n1) return false
    item(count) = i
    count += 1
    true
  }

  /**
   *  出栈
   * @return String
   */
  def pop(): String = {
    if (count == 0) return null
    val strItem = item(count - 1)
    count -= 1
    strItem
  }

}

object run {
  def main(args: Array[String]): Unit = {
    val arrayStack = new ArrayStackT(3)
    arrayStack.push("A")
    arrayStack.push("C")
    arrayStack.push("D")
    arrayStack.item.foreach(println)
    println("---------------------------")
    println(arrayStack.pop())
  }
}



